/**
 * LAZYLOAD
 */
LazyLoad=function(x,h){function r(b,a){var c=h.createElement(b),d;for(d in a)a.hasOwnProperty(d)&&c.setAttribute(d,a[d]);return c}function k(b){var a=i[b],c,d;if(a){c=a.callback;d=a.urls;d.shift();l=0;if(!d.length){c&&c.call(a.context,a.obj);i[b]=null;j[b].length&&m(b)}}}function w(){if(!e){var b=navigator.userAgent;e={async:h.createElement("script").async===true};(e.webkit=/AppleWebKit\//.test(b))||(e.ie=/MSIE/.test(b))||(e.opera=/Opera/.test(b))||(e.gecko=/Gecko\//.test(b))||(e.unknown=true)}}function m(b,
a,c,d,s){var n=function(){k(b)},o=b==="css",f,g,p;w();if(a){a=typeof a==="string"?[a]:a.concat();if(o||e.async||e.gecko||e.opera)j[b].push({urls:a,callback:c,obj:d,context:s});else{f=0;for(g=a.length;f<g;++f)j[b].push({urls:[a[f]],callback:f===g-1?c:null,obj:d,context:s})}}if(!(i[b]||!(p=i[b]=j[b].shift()))){q||(q=h.head||h.getElementsByTagName("head")[0]);a=p.urls;f=0;for(g=a.length;f<g;++f){c=a[f];if(o)c=r("link",{charset:"utf-8","class":"lazyload",href:c,rel:"stylesheet",type:"text/css"});else{c=
r("script",{charset:"utf-8","class":"lazyload",src:c});c.async=false}if(e.ie)c.onreadystatechange=function(){var t=this.readyState;if(t==="loaded"||t==="complete"){this.onreadystatechange=null;n()}};else if(o&&(e.gecko||e.webkit))if(e.webkit){p.urls[f]=c.href;u()}else setTimeout(n,50*g);else c.onload=c.onerror=n;q.appendChild(c)}}}function u(){var b=i.css,a;if(b){for(a=v.length;a&&--a;)if(v[a].href===b.urls[0]){k("css");break}l+=1;if(b)l<200?setTimeout(u,50):k("css")}}var e,q,i={},l=0,j={css:[],js:[]},
v=h.styleSheets;return{css:function(b,a,c,d){m("css",b,a,c,d)},js:function(b,a,c,d){m("js",b,a,c,d)}}}(this,this.document);

/**
 * EOF LAZYLOAD
 */

/////////////////////////////////////////////////////////////////////////////////

var CAuth = {
	"__obj" : null,
	"init" : function(svc) {
		CAuth.__obj = new CAuthSingleton(svc);
	},
	"login" : function(cb) {
		CAuth.__obj.login(cb);
	},
	"logout" : function(cb) {
		CAuth.__obj.logout(cb);
	},
	"prompt" : function(cb) {
		CAuth.__obj.prompt(cb);
	},
	"verify" : function(cookie) {
		CAuth.__obj.verify(cookie);
	}
}

/////////////////////////////////////////////////////////////////////////////////


var CAuthSingleton = function(serviceAddress){
	var 
		svc = null,
		callback = null,
		cookie = null,
		win = null,
		_debug = false
	
	init(serviceAddress);
	
	this.verify = function(what) {
		debug("Verifying cookie: "+ ((what)? "Exists (setting cookie)" : "Doesn't exist"))
		cookie = what;
		var isLoggedIn = (cookie) ? true : false;
		callback(isLoggedIn);
	}
	
	this.login = function(cb) {
		callback = cb;
		
		if (cookie) {
			var isLoggedIn = (cookie) ? true : false;
			callback(isLoggedIn);
		}
		else {
			connect();
		}
	}
	
	this.logout = function(cb) {
		if (!cookie) return false;
		cookie = 0;
		callback = cb;
		disconnect();
	}
	
	this.prompt = function(cb) {
		callback = cb;
		win = window.open(svc+"/prompt","mywindow","location=0,status=0,scrollbars=0, width=320,height=200");
		var that = this;
		setTimeout(function() { _checkWindow() }, 500);
	}
	
	function debug(txt) {
		if (!_debug) return;
		if (typeof console !== "undefined") console.log(txt)
	}
	
	function init(serviceAddress) {
		svc = serviceAddress;
	}
	
	function connect() {
		
		if (!svc) {
			debug("CAuth: Service not defined!");
			return;
		}
		else if (!callback) {
			debug("CAuth: Callback not defined!");
			return;
		}
		
		LazyLoad.js(svc + '?'+_randSeed());
	}
	
	function disconnect() {
		if (!svc) {
			debug("CAuth: Service not defined!");
			return;
		}
		else if (!callback) {
			debug("CAuth: Callback not defined!");
			return;
		}
		
		LazyLoad.js(svc +'/logout' + '?'+_randSeed(), callback);
	}
	
	
	function _checkWindow() {
		if (!win.closed) {
			setTimeout(_checkWindow, 500);
		}
		else {
			CAuth.login(callback);
		}
	}
	
	function _randSeed() {
		var text = "";
	    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.,";
	
	    for( var i=0; i < 40; i++ )
	        text += possible.charAt(Math.floor(Math.random() * possible.length));
	
	    return new Date().getTime()+text;
	}
}